package com.System.servlet;

import com.System.pojo.*;
import com.System.service.Bar;
import com.System.service.BarImpl.BarService;
import com.System.service.PostImpl.PostService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/Bar/*")
public class BarServlet extends BaseServlet{
    Bar service=new BarService();

    public void selectOne(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = request.getParameter("barName");
        com.System.pojo.Bar bar = service.selectOne(name);
        // 设置日期格式化样式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将 bar 对象转换为 JSONObject



        // 格式化创建时间并添加到 JSONObject 中
        String createdTime = dateFormat.format(bar.getCreated_at());
        bar.setCreated_at(null);
        JSONObject barObject = (JSONObject) JSON.toJSON(bar);
        barObject.put("created_at", createdTime);

        // 将整合后的 JSONObject 转换为字符串输出
        String jsonString = barObject.toJSONString();

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void selectAllMyPosts(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        List<Post> posts = service.selectAllPosts(new Post(username,barName));
        // 设置日期格式化样式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将 bar 对象转换为 JSONObject

        for (Post post : posts) {
            String createdTime = dateFormat.format(post.getCreated_at());
            post.setCreated_at(null);
            JSONObject barObject = (JSONObject) JSON.toJSON(post);
            barObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = barObject.toJSONString();

            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }

    }
    public void selectOutOfMyPosts(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        List<Post> posts = service.selectOutOfMyPosts(new Post(username,barName));
        // 设置日期格式化样式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将 bar 对象转换为 JSONObject

        for (Post post : posts) {
            String createdTime = dateFormat.format(post.getCreated_at());
            post.setCreated_at(null);
            JSONObject barObject = (JSONObject) JSON.toJSON(post);
            barObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = barObject.toJSONString();

            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }
    public void addFocus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        com.System.pojo.Bar result = service.selectNowNumber(new com.System.pojo.Bar(barName, 0));
        service.updateNowNumber(new com.System.pojo.Bar(barName,result.getNowNumber()+1));
        service.addFocus(new User_Bar_Relationship(username,barName,0));
        response.getWriter().write("success");
    }

        public void deleteFocus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        com.System.pojo.Bar result = service.selectNowNumber(new com.System.pojo.Bar(barName, 0));
        service.updateNowNumber(new com.System.pojo.Bar(barName,result.getNowNumber()-1));
        service.deleteFocus(new User_Bar_Relationship(username,barName,0));
        response.getWriter().write("success");

    }
    public void hasFocus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        User_Bar_Relationship flag =null;
        flag=service.hasFocus(new User_Bar_Relationship(username, barName, 0));
        if(flag==null){

            response.getWriter().write("no");
        }
        else {

            response.getWriter().write("yes");

        }
    }

    public void addPinPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        Post post = JSON.parseObject(param, Post.class);
        post.setBarName(barName);
        Post result = service.hasPinPost(barName);
        if(result!=null)
            service.deletePinPost(barName);
        service.addPinPost(post);
        response.getWriter().write("success");
    }

    public void selectPinPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        Post pinPost= service.selectPinPost(barName);
        if (pinPost!=null){
        // 设置日期格式化样式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将 bar 对象转换为 JSONObject



        // 格式化创建时间并添加到 JSONObject 中
        String createdTime = dateFormat.format(pinPost.getCreated_at());
        pinPost.setCreated_at(null);
        JSONObject barObject = (JSONObject) JSON.toJSON(pinPost);
        barObject.put("created_at", createdTime);

        // 将整合后的 JSONObject 转换为字符串输出
        String jsonString = barObject.toJSONString();

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
        }
    }
    public void deletePinPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        service.deletePinPost(barName);
        response.getWriter().write("success");
    }

    public void updateBarOwner(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        Post post = JSON.parseObject(param, Post.class);
        String newOwner = post.getName();

        Ban ban = service.hasBan(new Ban(newOwner, barName));
        if(ban!=null){
            service.deleteBan(ban);
        }

        User_Bar_Relationship userBarRelationship=new User_Bar_Relationship(username,barName,0);
        service.updateBarOwner(userBarRelationship);

        User_Bar_Relationship result = service.hasFocus(new User_Bar_Relationship(newOwner, barName));
        if(result!=null){
            service.updateBarOwner(new User_Bar_Relationship(newOwner,barName,1));
        }else{
            com.System.pojo.Bar temp = service.selectNowNumber(new com.System.pojo.Bar(barName, 0));
            service.updateNowNumber(new com.System.pojo.Bar(barName,temp.getNowNumber()+1));
            service.addRelationShip(new User_Bar_Relationship(newOwner,barName,1));
        }
        service.updateBarTableOwner(new User_Bar_Relationship(post.getName(),barName));
        response.getWriter().write("success");
    }
    public void updateBan(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        Post post = JSON.parseObject(param, Post.class);
        String name = post.getName();
        Ban ban = new Ban(name, barName);
        Ban result = service.hasBan(ban);
        if(result==null){
            service.addBan(ban);
            Report report=new Report();
            report.setBarName(barName);
            report.setUserName(post.getName());
            report.setPostId(post.getId());
            Report temp = service.hasReport(report);
            if(temp!=null){
                service.deleteReport(temp);
            }
            response.getWriter().write("success");
        }else{
            service.deleteBan(result);
            response.getWriter().write("Delete success");
        }

    }
    public void addReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");
        com.System.service.Post temp=new PostService();
        System.out.println(postId);
        Post post = temp.selectOne(new Post(Integer.parseInt(postId)));
        System.out.println(post.toString());
        int id=Integer.parseInt(postId);
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        Report report= JSON.parseObject(param, Report.class);
        System.out.println(report.toString());
        System.out.println(report.getReason());
        report.setPostId(id);
        report.setBarName(barName);
        report.setUserName(username);
        report.setContext(post.getContext());
        report.setBeReport(post.getName());
        Report result = service.hasReport(report);
        if(result!=null){
            response.getWriter().write("You has Add");
        }else{
            System.out.println(report.toString());
            service.addReport(report);
            response.getWriter().write("success");
        }
    }
    public void selectAllReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        Report report=new Report();
        report.setBarName(barName);
        System.out.println(barName);
        List<Report> reports = service.selectAllReport(report);
        System.out.println("执行了");
        for (Report report1 : reports) {

            System.out.println(report1.toString());
        }
        String jsonString = JSON.toJSONString(reports);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    }
